package com.practice.order.exception;

import com.alibaba.csp.sentinel.adapter.spring.webmvc_v6x.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.practice.order.common.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import java.io.PrintWriter;

@Component
public class MyBlockExceptionHangler implements BlockExceptionHandler {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       String resourceName, BlockException e) throws Exception {
        PrintWriter writer = response.getWriter();
        response.setContentType("application/json;chartset=utf-8");

        response.setStatus(429);//too many request

        R error = R.error(500, resourceName + "被 sentinel 限制了，原因： "+ e.getClass());

        String json = objectMapper.writeValueAsString(error);

        writer.write(json);

        writer.flush();

        writer.close();
    }
}
